<?php
/**
 * Created by PhpStorm.
 * User: kun
 * Date: 26/6/19
 * Time: 18:20
 */

namespace app\api\controller\v1;


use app\api\controller\Common;
use app\common\lib\Aes;


class User extends AuthBase
{
    //  测试
    public function test()
    {
        halt($this->user->toArray());
    }


    /**
     * 获取用户信息
     * 用户的基本信息非常隐私，需要加密处理
     */
    public function read()
    {
        $obj = new Aes();
        return show(config('code.success'), 'ok', $obj->encrypt($this->user));
    }

    /**
     * app查询用户名称是否可用
     * @return array
     */
    public function name()
    {
        $username = input('param.username');
        try {
            $id = model('User')->where('username', $username)->find();
        } catch (\Exception $e) {
            return show(config('code.success'), $e->getMessage(), [], 401);
        }

        if (is_null($id)) {
            return show(config('code.success'), 'ok', [], 200);
        } else {
            return show(config('code.error'), 'on', [], 200);
        }
    }

    /**
     * app用户退出登陆接口
     */
    public function logout()
    {
        $data = [
            'token' => '111',
        ];
        try {
            $id = model('User')->save($data, ['username' => $this->user->username]);
        } catch (\Exception $e) {
            return show(config('code.success'), $e->getMessage(), [], 401);
        }

        if ($id) {
            return show(config('code.success'), 'ok', 200);
        } else {
            return show(config('code.error'), '推出失败', [], 403);
        }

    }


}

